package com.bootdo.system.dao;

import com.bootdo.system.domain.ShareRecord;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface ShareRecordMapper {

	@Select("select * from tb_shareRecord where id = #{id}")
	ShareRecord get(Long id);
	
	@Select("<script>" +
	"select * from tb_shareRecord " + 
			"<where>" + 
			 "<if test=\"id != null and id != ''\">"+ "and id = #{id} " + "</if>" + 
	  		  "<if test=\"shareUserId != null and shareUserId != ''\">"+ "and shareUserId = #{shareUserId} " + "</if>" + 
	  		  "<if test=\"courseId != null and courseId != ''\">"+ "and courseId = #{courseId} " + "</if>" + 
		  	"</where>"+ 
			" <choose>" + 
	            "<when test=\"sort != null and sort.trim() != ''\">" + 
	                "order by ${sort} ${order}" + 
	            "</when>" + 
				"<otherwise>" + 
	                "order by id desc" + 
				"</otherwise>" + 
	        "</choose>"+
			"</script>")
	List<ShareRecord> list(Map<String,Object> map);
	
	@Insert("insert into tb_shareRecord (`shareUserId`, `courseId`, `redBagCount`,  `createUserId`, `createTime`)"
	+ "values (#{shareUserId}, #{courseId}, #{redBagCount},#{createUserId}, #{createTime})")
	@Options(useGeneratedKeys=true)
	int save(ShareRecord bComments);
	
	@Update("<script>"+ 
			"update tb_shareRecord " + 
					"<set>" + 
		            "<if test=\"id != null\">`id` = #{id}, </if>" + 
                    "<if test=\"shareUserId != null\">`shareUserId` = #{shareUserId}, </if>" + 
                    "<if test=\"courseId != null\">`courseId` = #{courseId}, </if>" + 
                    "<if test=\"redBagCount != null\">`redBagCount` = #{redBagCount}, </if>" + 
          			"</set>" + 
					"where id = #{id}"+
			"</script>")
	int update(ShareRecord bComments);
	
	@Update("update tb_shareRecord set redBagCount=redBagCount-1 where id=#{id}")
	int minusCount(Long id);
	
	@Delete("delete from tb_shareRecord where id =#{id}")
	int remove(Long id);
	
}
